home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / vector / DoubleVec.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  10KB  |  279 lines

  1. #ifndef    DOUBLEVEC_H
  2. #define    DOUBLEVEC_H
  3.  
  4. /* DoubleVec.h -- Double Precision Vectors
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.     K. E. Gorlen
  15.     Bg. 12A, Rm. 2033
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-1111
  21.     uucp: uunet!nih-csl!kgorlen
  22.     Internet:kgorlen@alw.nih.gov
  23.  
  24. Function:
  25.     
  26. Modification History:
  27.  
  28. $Log:    DoubleVec.h,v $
  29.  * Revision 3.0  90/05/16  23:00:29  kgorlen
  30.  * Release for 1st edition.
  31.  * 
  32. */
  33. #include "Vector.h"
  34. #include "BitVec.h"
  35. #include "IntVec.h"
  36.  
  37. class DoubleSlice;
  38. class DoublePick;
  39. class DoubleSlct;
  40.  
  41. class DoubleVec : public Vector {
  42.     DECLARE_MEMBERS(DoubleVec);
  43.     double* v;    // pointer to data, NULL if empty vector
  44.     void indexRangeErr() const;
  45. protected:
  46.     virtual void storer(OIOofd&) const;
  47.     virtual void storer(OIOout&) const;
  48. public:
  49.     DoubleVec(unsigned len =0);
  50.     DoubleVec(unsigned len, double from, double by =1.0);
  51.     DoubleVec(const double*, unsigned len);
  52.     DoubleVec(const DoubleVec&);
  53.     DoubleVec(const DoubleSlice&);
  54.     DoubleVec(const FloatVec&);
  55.     ~DoubleVec()        { delete v; }
  56.     DoubleSlice operator()(int pos, unsigned lgt, int stride =1);
  57.     const DoubleSlice operator()(int pos, unsigned lgt, int stride =1) const;
  58.     double*    pt()            { return v; }
  59.     const double* pt() const    { return v; }
  60.     operator DoubleSlice();
  61.     operator const DoubleSlice() const;
  62. //    operator ComplexVec();
  63.     double&    operator[](int i) {        // vector element
  64.         if ((unsigned)i >= n) indexRangeErr();
  65.         return v[i];
  66.     }
  67.     const double& operator[](int i) const {    // vector element
  68.         if ((unsigned)i >= n) indexRangeErr();
  69.         return v[i];
  70.     }
  71.     double& operator()(int i)        { return v[i]; }
  72.     const double& operator()(int i) const    { return v[i]; }
  73.     DoublePick operator[](const IntVec&);
  74.     const DoublePick operator[](const IntVec&) const;
  75.     DoubleSlct operator[](const BitVec&);
  76.     const DoubleSlct operator[](const BitVec&) const;
  77.     void /*DoubleVec::*/operator=(const DoubleVec&);
  78.     void /*DoubleVec::*/operator=(const DoubleSlice&);
  79.     void /*DoubleVec::*/operator=(const DoubleSlct&);
  80.     void /*DoubleVec::*/operator=(const DoublePick&);
  81.     void /*DoubleVec::*/operator=(double);
  82.     void /*DoubleVec::*/lengthErr(const DoubleSlice&) const;
  83.     void selectErr(const BitVec&) const;
  84.     virtual void    deepenShallowCopy();
  85.     virtual unsigned hash() const;
  86.     virtual bool    isEqual(const Object&) const;
  87.     virtual void    printOn(ostream& strm =cout) const;
  88.     virtual void    scanFrom(istream& strm);
  89.     virtual void    sort();
  90.     virtual const Class* species() const;
  91. };
  92.  
  93. class TempDoubleVec : public DoubleVec {
  94.     friend DoubleSlice;
  95.     friend DoublePick;
  96.     friend DoubleSlct;
  97.     TempDoubleVec(unsigned len =0) : DoubleVec(len) {}
  98.     virtual void free();
  99. };
  100.  
  101. class DoubleSlice : public NIHCL {
  102.     DoubleVec* V;    // vector pointer
  103.     double* p;    // slice pointer
  104.     unsigned l;    // slice length
  105.     int k;        // slice stride
  106.     DoubleSlice(const DoubleVec& v, int pos, unsigned lgt, int stride =1);
  107.     DoubleSlice(const DoubleVec& v, unsigned lgt) {
  108.         V = &(DoubleVec&)v;  p = ((DoubleVec&)v).pt();  l = lgt;  k = 1;
  109.     }
  110.     DoubleSlice(const DoubleSlice&);
  111.     friend DoubleVec;
  112. public:
  113.     DoubleSlice(const DoublePick&);
  114.     DoubleSlice(const DoubleSlct&);
  115.     ~DoubleSlice()        { V->free(); }
  116.     double*    pt()            { return p; }
  117.     const double* pt() const    { return p; }
  118.     unsigned length() const    { return l; }
  119.     int stride() const    { return k; }
  120.     void /*DoubleSlice::*/operator=(const DoubleVec&);
  121.     void /*DoubleSlice::*/operator=(const DoublePick&);
  122.     void /*DoubleSlice::*/operator=(const DoubleSlct&);
  123.     void /*DoubleSlice::*/operator=(const DoubleSlice&);
  124.     void /*DoubleSlice::*/operator=(double);
  125.     void /*DoubleSlice::*/lengthErr(const DoubleVec&) const;
  126.     void /*DoubleSlice::*/lengthErr(const DoubleSlice&) const;
  127.     void /*DoubleSlice::*/lengthErr(const IntVec&) const;
  128.     void selectErr(const BitVec&) const;
  129. friend    DoubleVec    operator-(const DoubleSlice&);
  130. friend    DoubleVec    operator++(DoubleSlice&);
  131. friend    DoubleVec    operator--(DoubleSlice&);
  132. friend    DoubleVec    operator*(const DoubleSlice&,const DoubleSlice&);
  133. friend    DoubleVec    operator/(const DoubleSlice&,const DoubleSlice&);
  134. friend    DoubleVec    operator+(const DoubleSlice&,const DoubleSlice&);
  135. friend    DoubleVec    operator-(const DoubleSlice&,const DoubleSlice&);
  136. friend    DoubleVec    operator*(const DoubleSlice&,double);
  137. friend    DoubleVec    operator*(double s,const DoubleSlice& V)  { return V*s; }
  138. friend    DoubleVec    operator/(const DoubleSlice&,double);
  139. friend    DoubleVec    operator/(double,const DoubleSlice&);
  140. friend    DoubleVec    operator+(const DoubleSlice&,double);
  141. friend    DoubleVec    operator+(double s,const DoubleSlice& V)  { return V+s; }
  142. friend    DoubleVec    operator-(const DoubleSlice&,double);
  143. friend    DoubleVec    operator-(double,const DoubleSlice&);
  144. friend    BitVec        operator<(const DoubleSlice&,const DoubleSlice&);
  145. friend    BitVec        operator>(const DoubleSlice& U,const DoubleSlice& V)  { return V < U; }
  146. friend    BitVec        operator<=(const DoubleSlice&,const DoubleSlice&);
  147. friend    BitVec        operator>=(const DoubleSlice& U,const DoubleSlice& V)  { return V <= U; }
  148. friend    BitVec        operator==(const DoubleSlice&,const DoubleSlice&);
  149. friend    BitVec        operator!=(const DoubleSlice&,const DoubleSlice&);
  150. friend    BitVec        operator<(const DoubleSlice&,double);
  151. friend    BitVec        operator<(double s,const DoubleSlice& V)  { return V>s; }
  152. friend    BitVec        operator>(const DoubleSlice&,double);
  153. friend    BitVec        operator>(double s,const DoubleSlice& V)  { return V<s; }
  154. friend    BitVec        operator<=(const DoubleSlice&,double);
  155. friend    BitVec        operator<=(double s,const DoubleSlice& V)  { return V>=s; }
  156. friend    BitVec        operator>=(const DoubleSlice&,double);
  157. friend    BitVec        operator>=(double s,const DoubleSlice& V)  { return V<=s; }
  158. friend    BitVec        operator==(const DoubleSlice&,double);
  159. friend    BitVec        operator==(double s,const DoubleSlice& V)  { return V==s; }
  160. friend    BitVec        operator!=(const DoubleSlice&,double);
  161. friend    BitVec        operator!=(double s,const DoubleSlice& V)  { return V!=s; }
  162. friend    void        operator+=(DoubleSlice&,const DoubleSlice&);
  163. friend    void        operator+=(DoubleSlice&,double);
  164. friend    void        operator-=(DoubleSlice&,const DoubleSlice&);
  165. friend    void        operator-=(DoubleSlice&,double);
  166. friend    void        operator*=(DoubleSlice&,const DoubleSlice&);
  167. friend    void        operator*=(DoubleSlice&,double);
  168. friend    void        operator/=(DoubleSlice&,const DoubleSlice&);
  169. friend    void        operator/=(DoubleSlice&,double);
  170.     DoubleVec    apply(mathFunTy) const;
  171. friend    DoubleVec    abs(const DoubleSlice& V);
  172. friend    DoubleVec    acos(const DoubleSlice& V)    { return V.apply(acos); }
  173. friend    DoubleVec    asin(const DoubleSlice& V)    { return V.apply(asin); }
  174. friend    DoubleVec    atan(const DoubleSlice& V)    { return V.apply(atan); }
  175. friend    DoubleVec    atan2(const DoubleSlice&,const DoubleSlice&);
  176. friend    DoubleVec    ceil(const DoubleSlice& V)    { return V.apply(ceil); }
  177. friend    DoubleVec    cos(const DoubleSlice& V)    { return V.apply(cos); }
  178. friend    DoubleVec    cosh(const DoubleSlice& V)    { return V.apply(cosh); }
  179. friend    DoubleVec    cumsum(const DoubleSlice&);
  180. friend    DoubleVec    delta(const DoubleSlice&);
  181. friend    double        dot(const DoubleSlice&,const DoubleSlice&);
  182. friend    DoubleVec    exp(const DoubleSlice& V)    { return V.apply(exp); }
  183. friend    DoubleVec    floor(const DoubleSlice& V)    { return V.apply(floor); }
  184. friend    DoubleVec    log(const DoubleSlice& V)    { return V.apply(log); }
  185. friend    int        max(const DoubleSlice&);
  186. friend    int        min(const DoubleSlice&);
  187. friend    double        prod(const DoubleSlice&);
  188. friend    DoubleVec    pow(const DoubleSlice&,const DoubleSlice&);
  189. friend    DoubleVec    reverse(const DoubleSlice&);
  190. friend    DoubleVec    sin(const DoubleSlice& V)    { return V.apply(sin); }
  191. friend    DoubleVec    sinh(const DoubleSlice& V)    { return V.apply(sinh); }
  192. friend    DoubleVec    sqrt(const DoubleSlice& V)    { return V.apply(sqrt); }
  193. friend    double        sum(const DoubleSlice&);
  194. friend    DoubleVec    tan(const DoubleSlice& V)    { return V.apply(tan); }
  195. friend    DoubleVec    tanh(const DoubleSlice& V)    { return V.apply(tanh); }
  196. };
  197.  
  198. class DoublePick : public NIHCL {
  199.     DoubleVec* V;
  200.     const IntVec* X;
  201.     DoublePick(const DoubleVec& v,const IntVec& x)    { V = &(DoubleVec&)v;  X = &x; }
  202.     DoublePick(const DoublePick& s)            { V = s.V; X = s.X; }
  203.     friend DoubleVec;
  204.     friend DoubleSlice;
  205.     friend DoubleSlct;
  206. public:
  207.     void /*DoublePick::*/operator=(const DoubleVec&);
  208.     void /*DoublePick::*/operator=(const DoublePick&);
  209.     void /*DoublePick::*/operator=(const DoubleSlct&);
  210.     void /*DoublePick::*/operator=(const DoubleSlice&);
  211.     void /*DoublePick::*/operator=(double);
  212.     unsigned length() const    { return X->length(); }
  213. };
  214.  
  215. class DoubleSlct: public NIHCL {
  216.     DoubleVec* V;
  217.     const BitVec* B;
  218.     DoubleSlct(const DoubleVec& v, const BitVec& b)    { V = &(DoubleVec&)v;  B = &b; }
  219.     DoubleSlct(const DoubleSlct& s)            { V = s.V; B = s.B; }
  220.     friend DoubleVec;
  221.     friend DoubleSlice;
  222.     friend DoublePick;
  223. public:
  224.     void /*DoubleSlct::*/operator=(const DoubleVec&);
  225.     void /*DoubleSlct::*/operator=(const DoublePick&);
  226.     void /*DoubleSlct::*/operator=(const DoubleSlct&);
  227.     void /*DoubleSlct::*/operator=(const DoubleSlice&);
  228.     void /*DoubleSlct::*/operator=(double);
  229.     unsigned length() const    { return B->length(); }
  230. };
  231.  
  232. inline DoubleSlice DoubleVec::operator()(int pos, unsigned lgt, int stride)
  233. {
  234.     DoubleSlice s(*this,pos,lgt,stride);
  235.     return s;
  236. }
  237.  
  238. inline const DoubleSlice DoubleVec::operator()(int pos, unsigned lgt, int stride) const
  239. {
  240.     const DoubleSlice s(*this,pos,lgt,stride);
  241.     return s;
  242. }
  243.  
  244. inline DoubleVec::operator DoubleSlice()
  245. {
  246.     DoubleSlice s(*this,length());
  247.     return s;
  248. }
  249.  
  250. inline DoubleVec::operator const DoubleSlice() const
  251. {
  252.     const DoubleSlice s(*this,length());
  253.     return s;
  254. }
  255.  
  256. inline DoublePick DoubleVec::operator[](const IntVec& I)
  257. {
  258.     return DoublePick(*this,I);
  259. }
  260.  
  261. inline const DoublePick DoubleVec::operator[](const IntVec& I) const
  262. {
  263.     const DoublePick t(*this,I);
  264.     return t;
  265. }
  266.  
  267. inline DoubleSlct DoubleVec::operator[](const BitVec& B)
  268. {
  269.     return DoubleSlct(*this,B);
  270. }
  271.  
  272. inline const DoubleSlct DoubleVec::operator[](const BitVec& B) const
  273. {
  274.     const DoubleSlct t(*this,B);
  275.     return t;
  276. }
  277.  
  278. #endif
  279.